package com.weed.helper;

import java.util.*;
import org.jdom.input.SAXBuilder;
import java.io.*;
import org.jdom.*;
import javax.naming.*;

/**
 *
 * <p>Title: 取系统自定义文件esim.xml的配置信息并封装成类</p>
 * <p>Description: 兼容多种J2EE容器的部署</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: 新意科技</p>
 * @author :
 * @version 1.0
 */

public class EJBConfigHelper {
  private String EJB_NAME;
  private String DEPLOY_TYPE;
  private String EJB_URL;
  private String DB_TYPE;
  private String DB_NAME;
  private int DB_BUG = 0;
  private Context CONTEXT;
  private static Context ctx;
  private static Hashtable ht = null;

  private String CONTAINER_NAME;
  private String EJB_JNDI_PREFIX;
  private String INITIAL_CONTEXT_FACTORY;
  private String RMI_PROTOCOL_PREFIX;
  private String LOCAL_EJB_JNDI_PREFIX;
  private String LOCAL_EJB_JNDI_SUFFIX;
  private String JDBC_JNDI_PREFIX;

  public EJBConfigHelper() {
  }

  public EJBConfigHelper(String EJB_NAME) {
    this.EJB_NAME = EJB_NAME;
  }

  public String getEJB_NAME() {
    return EJB_NAME;
  }

  public void setEJB_NAME(String EJB_NAME) {
    this.EJB_NAME = EJB_NAME;
  }

  public String getDB_NAME() {
    return DB_NAME;
  }

  public void setDB_NAME(String DB_NAME) {
    this.DB_NAME = DB_NAME;
  }

  public String getDEPLOY_TYPE() {
    return DEPLOY_TYPE;
  }

  public void setDEPLOY_TYPE(String DEPLOY_TYPE) {
    this.DEPLOY_TYPE = DEPLOY_TYPE;
  }

  public String getEJB_URL() {
    return EJB_URL;
  }

  public void setEJB_URL(String EJB_URL) {
    this.EJB_URL = EJB_URL;
  }

  public String getDB_TYPE() {
    return DB_TYPE;
  }

  public void setDB_TYPE(String DB_TYPE) {
    this.DB_TYPE = DB_TYPE;
  }

  public int getDB_BUG() {
    return DB_BUG;
  }

  public void setDB_BUG(int DB_BUG) {
    this.DB_BUG = DB_BUG;
  }

  public Context getCONTEXT() {
    return CONTEXT;
  }

  public void setCONTEXT(Context CONTEXT) {
    this.CONTEXT = CONTEXT;
  }

  public String getCONTAINER_NAME() {
    return CONTAINER_NAME;
  }

  public void setCONTAINER_NAME(String CONTAINER_NAME) {
    this.CONTAINER_NAME = CONTAINER_NAME;
  }

  public String getEJB_JNDI_PREFIX() {
    return EJB_JNDI_PREFIX;
  }

  public void setEJB_JNDI_PREFIX(String EJB_JNDI_PREFIX) {
    this.EJB_JNDI_PREFIX = EJB_JNDI_PREFIX;
  }

  public String getINITIAL_CONTEXT_FACTORY() {
    return INITIAL_CONTEXT_FACTORY;
  }

  public void setINITIAL_CONTEXT_FACTORY(String INITIAL_CONTEXT_FACTORY) {
    this.INITIAL_CONTEXT_FACTORY = INITIAL_CONTEXT_FACTORY;
  }

  public String getRMI_PROTOCOL_PREFIX() {
    return RMI_PROTOCOL_PREFIX;
  }

  public void setRMI_PROTOCOL_PREFIX(String RMI_PROTOCOL_PREFIX) {
    this.RMI_PROTOCOL_PREFIX = RMI_PROTOCOL_PREFIX;
  }

  public String getLOCAL_EJB_JNDI_PREFIX() {
    return LOCAL_EJB_JNDI_PREFIX;
  }

  public void setLOCAL_EJB_JNDI_PREFIX(String LOCAL_EJB_JNDI_PREFIX) {
    this.LOCAL_EJB_JNDI_PREFIX = LOCAL_EJB_JNDI_PREFIX;
  }

  public String getLOCAL_EJB_JNDI_SUFFIX() {
    return LOCAL_EJB_JNDI_SUFFIX;
  }

  public void setLOCAL_EJB_JNDI_SUFFIX(String LOCAL_EJB_JNDI_SUFFIX) {
    this.LOCAL_EJB_JNDI_SUFFIX = LOCAL_EJB_JNDI_SUFFIX;
  }

  public String getJDBC_JNDI_PREFIX() {
    return JDBC_JNDI_PREFIX;
  }

  public void setJDBC_JNDI_PREFIX(String JDBC_JNDI_PREFIX) {
    this.JDBC_JNDI_PREFIX = JDBC_JNDI_PREFIX;
  }

  public static Hashtable LoadXML() throws Exception {
    try {
      if (ht == null) {
        ht = new Hashtable();
        SAXBuilder builder = new SAXBuilder();
        InputStream is = new FileInputStream("conf\\esim.xml");
        Document doc = builder.build(is);

        //取J2EE容器的属性
        Element container = doc.getRootElement().getChild("container");
        String container_name = container.getChild("name").getText();
        String ejb_jndi_prefix = container.getChild("ejb-jndi-prefix").getText();
        String initial_context_factory = container.getChild(
            "initial-context-factory").getText();
        String rmi_protocol_prefix = container.getChild("rmi-protocol-prefix").
            getText();
        String local_ejb_jndi_prefix = container.getChild(
            "local-ejb-jndi-prefix").getText();
        String local_ejb_jndi_suffix = container.getChild(
            "local-ejb-jndi-suffix").getText();
        String jdbc_jndi_prefix = container.getChild("jdbc-jndi-prefix").
            getText();

        //取全局的数据库类型
        Element db_property = doc.getRootElement().getChild("db-property").
            getChild("db-type");
        String db_type = db_property.getText();

        //取全局的DEBUG类型
        Element log = doc.getRootElement().getChild("log").getChild("ejb").
            getChild("level");
        int ejb_level = Integer.parseInt(log.getText());
        //EJB定义
        List ejb_list = doc.getRootElement().getChildren("ejb-deploy");
        Element xml = null;
        EJBConfigHelper ejb_config = null;
        for (int i = 0; i < ejb_list.size(); i++) {
          ejb_config = new EJBConfigHelper();
          xml = (Element) ejb_list.get(i);
          ejb_config.setEJB_NAME(xml.getChild("ejb-name").getText());
          ejb_config.setDEPLOY_TYPE(xml.getChild("deploy-type").getText());
          ejb_config.setEJB_URL(xml.getChild("ejb-url").getText());
          ejb_config.setDB_NAME(xml.getChild("db-name").getText());

          if (db_type.equalsIgnoreCase("unknown")) {
            ejb_config.setDB_TYPE(xml.getChild("db-type").getText());
          }
          else {
            ejb_config.setDB_TYPE(db_type);
          }

          if (ejb_level == -1) {
            ejb_config.setDB_BUG(Integer.parseInt(xml.getChild("log-level").
                                                  getText()));
          }
          else {
            ejb_config.setDB_BUG(ejb_level);
          }

          ejb_config.setCONTAINER_NAME(container_name);
          ejb_config.setEJB_JNDI_PREFIX(ejb_jndi_prefix);
          ejb_config.setINITIAL_CONTEXT_FACTORY(initial_context_factory);
          ejb_config.setRMI_PROTOCOL_PREFIX(rmi_protocol_prefix);
          ejb_config.setLOCAL_EJB_JNDI_PREFIX(local_ejb_jndi_prefix);
          ejb_config.setLOCAL_EJB_JNDI_SUFFIX(local_ejb_jndi_suffix);
          ejb_config.setJDBC_JNDI_PREFIX(jdbc_jndi_prefix);

          ejb_config.setCONTEXT(getCxt(xml.getChild("deploy-type").getText(),
                                       xml.getChild("ejb-url").getText(),
                                       initial_context_factory));

          ht.put(xml.getChild("ejb-name").getText(), ejb_config);
        }
      }
    }
    catch (Exception e) {
      ht = null;
      throw new Exception(e.getMessage());
    }
    return ht;
  }

  public static Context getCxt(String DEPLOY_TYPE, String EJB_URL,
                               String INITIAL_CONTEXT_FACTORY) throws Exception {
    try {
      if (DEPLOY_TYPE.equals("local")) {
        ctx = new InitialContext();
      }
      else {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
        env.put(Context.PROVIDER_URL, EJB_URL);
        env.put(javax.naming.Context.SECURITY_PRINCIPAL, "oc4jadmin");
        env.put(javax.naming.Context.SECURITY_CREDENTIALS, "sspocc");
        ctx = new InitialContext(env);
      }
    }
    catch (Exception e) {
      throw new Exception(e.getMessage());
    }
    return ctx;
  }
}
